$(function() {
    $.ajaxSetup({
        dataType: 'json',
        timeout: 15 * 1000,
        xhrFields: {
            withCredentials: true
        },
        error: function() {
            layer.open({
                content: '网络链接错误',
                btn: '好的',
                yes: function(index) {
                    layer.close(index)
                }
            });
        }
    })

    $('#registerBtn').on('click', function(event) {
        event.preventDefault();
        var register = {
            accounts: $('#username').val(),
            pwd: $('#password').val(),
            againpwd: $('#repassword').val(),
            email: $('#email').val(),
            phone: $('#telephone').val(),
            qq: $('#qq').val(),
            realname: $('#realname').val(),
            idcard: $('#cardno').val()
        }
        if (!$('input[name="read"]').prop('checked')) {
            layer.open({
                content: '请认真阅读并接受<a href="http://www.shizhenjiankang.com/assets/js/xieyi.html" target="_blank" style="color:blue;">《时针健康科技用户服务协议》</a>',
                btn: '好的',
                yes: function(index) {
                    layer.close(index)
                }
            });
        } else {
            checkout()
        }
        $.get(urlStr + '/register.cspx', register, function(res) {
            console.log(res)

            layer.open({
                content: res.Content,
                btn: '好的',
                yes: function(index) {
                    layer.close(index)
                    if (res.Success) {
                        window.location.reload()
                        setCookie('accounts', $('#username').val(), 1)
                    }
                }
            });

            /*optional stuff to do after success */
        });

        // if (true) {}
        /* Act on the event */
    });

    function checkout() {
        var $register = [{
                checkVal: $('#cardno').val(),
                checkContent: '身份证未填写',
                dom: $('#cardno')
            },
            {
                checkVal: $('#realname').val(),
                checkContent: '真实姓名未填写',
                dom: $('#realname')
            },
            {
                checkVal: $('#email').val(),
                checkContent: '用户邮箱未填写',
                dom: $('#email')
            },
            {
                checkVal: $('#repassword').val(),
                checkContent: '两次密码不一致',
                dom: $('#repassword')
            },
            {
                checkVal: $('#password').val(),
                checkContent: '用户密码未填写',
                dom: $('#password')
            },
            {
                checkVal: $('#username').val(),
                checkContent: '用户名称未填写',
                dom: $('#username')
            },

        ]
        $register.forEach(function(ele, ind) {

            if (!ele.checkVal) {
                console.log(ind)
                layer.open({
                    content: ele.checkContent,
                    btn: '好的',
                    yes: function(index) {
                        layer.close(index)
                        ele.dom.focus()
                            // return
                    }
                });
                // return
            }
        })
    }
});